package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.AnswerQuestion;
import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IAnswerQuestionService;
import cn.wolfcode.trip.base.service.IQuestionService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("questions")
public class QuestionContorller {
    @Autowired
    private IQuestionService questionService;
    @Autowired
    private IAnswerQuestionService answerQuestionService;

    @GetMapping
    public List<Question> listStrategies(){
        List<Question> list = questionService.listAll();
        return list;
    }

    @PostMapping("/{user.id}")
    public JSONResult saveQuestion(Question question){
        JSONResult result = new JSONResult();
        questionService.save(question);
        return result;
    }

    /**
     * 通过问题的id 查询所有的回答
     * @param questionId
     * @return
     */
    @GetMapping("/{questionId}/answerer")
    public List<AnswerQuestion> answerQuestionList(@PathVariable Long questionId){
        List<AnswerQuestion> list = answerQuestionService.listAllByQuizzerId(questionId);
        if (list.size() == 0){
            ArrayList<AnswerQuestion> list1 = new ArrayList<>();
            Question question = questionService.selectByQuestionId(questionId);
            AnswerQuestion answerQuestion = new AnswerQuestion();
            answerQuestion.setQuestion(question);
            list1.add(answerQuestion);
            return list1;
        }
        return list;
    }
    @PostMapping("/answerer")
    public JSONResult saveAnswerQuestion(AnswerQuestion answerQuestion){
        if ("".equals(answerQuestion.getContent())){
            return new JSONResult().mark("请输入文字");
        }
        User user = UserContext.getCurrentUser();
        answerQuestion.setAnswerer(user);
        answerQuestionService.save(answerQuestion);
        return new JSONResult();
    }
}
